package cn.zcib.controller;

import cn.zcib.domain.Teacher;
import cn.zcib.service.TeacherService;

import java.util.Scanner;

/**
 * @Author Lucky赵
 * @Date 2021年08月11日 19:10
 */
public class TeacherController {
    private static final Scanner sc = new Scanner(System.in);
    private static final TeacherService teacherService = new TeacherService();
    private static final Teacher teacher = new Teacher();

    public void start() {
        teacherLoop:
        while (true) {
            System.out.println("--------欢迎来到 <教师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加教师  2.删除教师  3.修改教师  4.查看教师  5.退出");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    addTea();
                    break;
                case 2:
                    delTeaById();
                    break;
                case 3:
                    updateTea();
                    break;
                case 4:
                    findAllTea();
                    break;
                case 5:
                    System.out.println("感谢您使用教师管理");
                    break teacherLoop;
                default:
                    System.out.println("请输入正确的信息");
                    break;
            }
        }
    }

    private void findAllTea() {
        Teacher[] teachers = teacherService.findAllTea();
        //判断数组的内存信息
        if (teachers == null) {
            System.out.println("查无信息，请重试！");
            return;
        }
        System.out.println("编号\t姓名\t年龄\t生日");
        for (Teacher tea : teachers) {
            if (tea != null)
                System.out.println(tea.getId() + "\t" + tea.getName() + "\t" + tea.getAge() + "\t\t" + tea.getBirthday());
        }
    }

    private void updateTea() {
        String id = inputId();
        Teacher tea = inputInfo(id);
        teacherService.updateStu(id, tea);
        System.out.println("修改成功");
    }


    private void delTeaById() {
        String id = inputId();
        teacherService.delTeaById(id);
        //提示删除成功
        System.out.println("删除成功！");
    }

    private void addTea() {
        String id;
        while (true) {
            System.out.print("请输入教师id：");
            id = sc.next();

            boolean flag = teacherService.isExists(id);
            if (flag)
                System.out.println("编号已被占用，请重新输入！");
            else
                break;
        }
        Teacher tea = inputInfo(id);
        boolean result = teacherService.addTea(tea);
        if (result)
            System.out.println("添加成功！");
        else
            System.out.println("添加失败！");
    }

    private String inputId() {
        String id;
        while (true) {
            System.out.print("请输入教师编号：");
            id = sc.next();
            boolean exists = teacherService.isExists(id);
            //判断id是否在容器中存在，不存在就重新录入
            if (!exists)
                System.out.println("请您输入正确的id信息！");
            else
                break;
        }
        return id;
    }

    private Teacher inputInfo(String id) {
        System.out.print("请输入教师姓名：");
        String name = sc.next();
        System.out.print("请输入教师年龄：");
        int age = sc.nextInt();
        System.out.print("请输入教师生日：");
        String birthday = sc.next();

        teacher.setId(id);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);
        return teacher;
    }
}
